package com.example.demo.collerct;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.entity.Blog;

import com.example.demo.entity.Comment;
import com.example.demo.entity.Tag;
import com.example.demo.entity.Type;
import com.example.demo.service.Blogservice;
import com.example.demo.service.Commentsevice;
import com.example.demo.service.Tagservice;
import com.example.demo.service.Typeservice;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
public class Blogcollerct {
    @Autowired
    Blogservice blogservice;
    @Autowired
    Typeservice typeservice;
    @Autowired
    Tagservice tagservice;
    @Autowired
    Commentsevice commentsevice;
    @RequestMapping("/toindex")
   //前台
        public String Toindex(Model model,@RequestParam(defaultValue = "0") Integer typeId,@RequestParam(defaultValue = "0") int tagIds,@RequestParam(defaultValue = "1") Integer pageIndex){
        Page<Blog> page = new Page<>(pageIndex,7);
        List<Type> types = typeservice.selectApp();
        List<Tag> tags = tagservice.selectTagCount();
        List<Blog> recommendBlogs = blogservice.recommend();
        model.addAttribute("recommendBlogs",recommendBlogs);
        model.addAttribute("tags",tags);
        model.addAttribute("types",types);
        model.addAttribute("pageIndex",pageIndex);
        model.addAttribute("pageInfo", blogservice.selectA(page,typeId,tagIds));
        return "index";
    }
    @RequestMapping("typesss")
    public String l(Model model,@RequestParam(defaultValue = "0") Integer typeId,@RequestParam(defaultValue = "0") int tagIds,@RequestParam(defaultValue = "1") Integer pageIndex){
        Page<Blog> page = new Page<>(pageIndex,3);
        List<Tag> tags = tagservice.selectTagCount();
        List<Type> types = typeservice.selectApp();
        model.addAttribute("types",types);
        model.addAttribute("tags",tags);
        Page<Blog> page1 = blogservice.selectA(page, typeId,tagIds);
        model.addAttribute("pageInfo",page1);
        model.addAttribute("typeId",typeId);
        return "types";
    }
    @RequestMapping("tagsss")
    public String la(Model model,@RequestParam(defaultValue = "0") Integer typeId,@RequestParam(defaultValue = "0") int tagIds,@RequestParam(defaultValue = "1") Integer pageIndex){
        Page<Blog> page = new Page<>(pageIndex,3);
        List<Tag> tags = tagservice.selectTagCount();
        model.addAttribute("tags",tags);
        Page<Blog> page1 = blogservice.selectA(page, typeId,tagIds);

        model.addAttribute("pageInfo",page1 );
        model.addAttribute("tagIds",tagIds);
        return "tags";
    }

    @RequestMapping("archivesss1")
    public String plsskaq(Model model){
        int countasa = blogservice.countasa();
        Map<Integer, List<Blog>> selectma = blogservice.selectma();
        System.out.println(selectma.toString());
        model.addAttribute("countasa",countasa);
        model.addAttribute("archiveMap",selectma);
        return "archives1";
    }
    @RequestMapping("/toBlogentity")
    public String leijie(Long id, Model model, HttpSession session){
        Blog blog = blogservice.slsct(id);
        session.setAttribute("id",id);

        List<Comment> comments = commentsevice.sle(id);
        for (Comment comment : comments) {
            comment.setGetcoc(commentsevice.sl(comment.getId()));
        }
       model.addAttribute("comments",comments);
        model.addAttribute("blog",blog);
        session.setAttribute("blog",blog);

        return "blog";
    }
    @RequestMapping("/comments")
    public String you(Model model,Comment comment,HttpSession session){
        int insert = commentsevice.insert(comment);
        if (insert>0){
            return "forward:toBlogentity?id="+session.getAttribute("id");
        }else{
            return "forward:toindex";
        }
    }
    @RequestMapping("/about")
    public String NB(){
        return "about";
    }

    @RequestMapping("/searchaa")
    public String ala(String query,@RequestParam(defaultValue = "1") Integer pageindex,Model model){
        Page<Blog> page = new Page<>(pageindex,4);
   model.addAttribute("pageInfo",blogservice.selectAkaa(page,query));
        return "search";
    }

    @ResponseBody
    @RequestMapping("/delTo")
    public  void  j(int id){
        int dele = blogservice.dele(id);
        System.out.println(dele);

    }
    @RequestMapping("/inputBlog")
    public String a( Model model){
        List<Type> types = typeservice.selectAC();
        List<Tag> selectq = tagservice.selectq();
        model.addAttribute("typess",types);
        model.addAttribute("selectq",selectq);
        return "admin/blogs-input";
    }
    @RequestMapping("/insertBlog")
    public String aa( Model model,Blog blog){
        if ("on".equals(blog.getRecommend())){
            blog.setRecommend("1");
        }else {
            blog.setRecommend("0");
        }
        if ("on".equals(blog.getAppreciation())){
            blog.setAppreciation("1");
        }else {
            blog.setAppreciation("0");
        }
        if ("on".equals(blog.getShare_statement())){
            blog.setShare_statement("1");
        }else {
            blog.setShare_statement("0");
        }
        if ("on".equals(blog.getCommentabled())){
            blog.setCommentabled("1");
        }else {
            blog.setCommentabled("0");
        }
        int inse = blogservice.inse(blog);
        System.out.println(inse);
        return "forward:/blogsa";
    }
@RequestMapping("/toUpdateBlog")
    public String km(int id,Model model){
    List<Type> types = typeservice.selectAC();
    Blog blog = blogservice.selectToid(id);
    List<Tag> selectq = tagservice.selectq();

    model.addAttribute("typess",types);
    model.addAttribute("blog",blog);
    model.addAttribute("selectq",selectq);
    return "admin/blogs-input";
}
@RequestMapping("/blogupdate")
    public String la(Blog blog){
    if ("on".equals(blog.getRecommend())){
        blog.setRecommend("1");
    }else {
        blog.setRecommend("0");
    }
    if ("on".equals(blog.getAppreciation())){
        blog.setAppreciation("1");
    }else {
        blog.setAppreciation("0");
    }
    if ("on".equals(blog.getShare_statement())){
        blog.setShare_statement("1");
    }else {
        blog.setShare_statement("0");
    }
    if ("on".equals(blog.getCommentabled())){
        blog.setCommentabled("1");
    }else {
        blog.setCommentabled("0");
    }
    Date date=new Date();
    blog.setUpdate_time(date);
    int update = blogservice.update(blog);
    return "forward:/blogsa";
}
}
